package com.leo.manage_system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leo.manage_system.entity.Admin;
import com.leo.manage_system.entity.PageView;
import com.leo.manage_system.entity.UserWechat;
import com.leo.manage_system.mapper.PageViewMapper;
import com.leo.manage_system.service.AdminService;
import com.leo.manage_system.service.PageViewService;
import com.leo.manage_system.utils.JwtUtils;
import com.leo.manage_system.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 访问量 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-28
 */
@Api(tags = "页面访问量")
@RestController
@CrossOrigin //跨域
@RequestMapping("/service_manage/page-view")
public class PageViewController {
    JwtUtils jwtUtils = new JwtUtils();
    final PageViewService pageViewService;

    @Autowired
    public PageViewController(PageViewService pageViewService){
        this.pageViewService=pageViewService;
    }

    @ApiOperation(value = "查询访问量")
    @GetMapping("getPageView")
    public R getPageView(@ApiParam(name = "token", value = "token", required = true)
                             @RequestParam(value="token") String token) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {

            Date date = new Date(); // this object contains the current date value
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String s = sdf.format(new Date());
            try {
                date = sdf.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            List<Integer> list = pageViewService.getPageView(date);
            int total =  pageViewService.getSum();
            return R.ok().data("total",total).data("list", list);
        }

    }


}

